define(["jquery", "template", "bootstrap", "bbcare", "jquery.dataTables", 'bootstrap.dataTables',
    "scprit/assess/app/gesell/common.js",
    "oms_config/root"
], function($, template, bootstrap, $bb, $jTable, $bTable, $common, $root) {
    var tables = {};
    tables.init = function(type) {
        var URL = $bb.baseUrl("followapp");
        var sessionId = sessionStorage.sessionId;
        var oTable = $("#myEduTable").DataTable({
            pageLength: 10, //默认条数
            lengthMenu: [10, 30, 60], //每页显示记录数options选项
            info: true, //是否使用表格左下角的信息；
            destroy: true,
            stateSave: false,
            ordering: false, //是否允许Datatables开启排序
            paging: true, //默认true ,开启分页
            searching: false, //默认true,是否开启搜索
            serverSide: true, //是否启用服务器端处理模式，false,在客户端处理，true在服务器端处理；
            ajax: {
                url: URL + "/service/evaluation/recordUserlist?sessionId=" + sessionId,
                type: "post",
                contentType: "application/json;charset=UTF-8",
                dataType: "json",
                data: function(args) {
                    var config = $root.init();
                    var showFlag = config.dom.baileyScreeningShowCreate ? "1" : "0";
                    var data = JSON.stringify({
                        data: {
                            "args": JSON.stringify(args),
                            "channelType": "2",
                            "evaluationType": type,
                            "userName": $.trim($("#patientName").val()),
                            "phoneNo": $.trim($("#phoneNum").val()),
                            "beginTime": $("input[name=beginTime]").val(),
                            "endTime": $("input[name=endTime]").val(),
                        }
                    });
                    return data;
                },
                dataSrc: function(data) {
                    if (data.rows == 0) {
                        var phone = $.trim($("#phoneNum").val());
                        var name = $.trim($("#patientName").val());
                        if (phone) { $("#newTel").addClass("show"); }
                        if (name) { $("#newName").addClass("show"); }
                    } else {
                        $(".new").removeClass("show");
                    }
                    return data.rows;
                }
            },
            columns: [{
                    data: 'userName',
                    defaultContent: ""
                },
                {
                    data: 'phoneNo',
                    defaultContent: ""
                },
                {
                    data: function(row, type, set, meta) {
                        return row.gender == 'F' ? "女" : "男";
                    },
                    defaultContent: ""
                },
                {
                    data: 'age',
                    defaultContent: ""
                },
                {
                    data: 'testTime',
                    defaultContent: ""
                },
                {
                    data: function(row, type, set, meta) {
                        return '<a class="newEva">新建测评</a>&nbsp;' +
                            '<a class="historyEva">历史测评</a>'
                    },
                    defaultContent: ""
                }
            ],
            rowCallback: function(row, data, index) {
                $('td:eq(5) a', row).click(function() {
                    var attr = $(this).attr("class");
                    var babyId = data.userId;
                    var obj = {
                        "babyId": babyId,
                        "type": type
                    };
                    if (attr == "newEva") {
                        $bb.openPage("gesellDetail", obj);
                    } else {
                        $bb.openPage("gesellHistoryList", obj);
                    }
                });
            },
            initComplete: function(settings, json) {
                $("#phoneNum").on("input propertychange", function() {
                    var key = $(this).val();
                    var reg = /^[0-9]{0,11}$/;
                    if (!reg.test(key)) {
                        $bb.toastShow("请输入11位手机号码", 1500);
                        $(this).val("").focus();
                    } else {
                        oTable.ajax.reload();
                    }
                });
                $("#patientName").on("input propertychange", function() {
                    oTable.ajax.reload();
                });
                $("#btnSearch").click(function() {
                    oTable.ajax.reload();
                });
            }
        });
        var id = "#search" + type;
        $common.searchBoxInit({ "id": id });
        $('#myModal').on('hidden.bs.modal', function(e) {
            $("#searchUser").val("");
            $("#infoLists").removeClass("show").html("");
        });
        $("#searchUser").on("input propertychange", function() {
            var key = $.trim($(this).val());
            if (key) {
                search(key);
            } else {
                $("#infoLists").removeClass("show").html('');
            }
        });
        $("#toSearch").click(function() {
            var key = $.trim($("#searchUser").val());
            if (key) {
                search(key);
            } else {
                $("#infoLists").removeClass("show").html("");
            }
        });
        $("#newName").click(function() {
            var name = $.trim($("#patientName").val());
            $('#myModal').modal('show');
            $("#searchUser").val(name);
            search(name);
        });
        $("#newTel").click(function() {
            var phone = $.trim($("#phoneNum").val());
            $('#myModal').modal('show');
            $("#searchUser").val(phone);
            search(phone);
        });
        //输入内容先按照姓名查询，如果查询不到按照手机号查询: flag：手机号：1；不传：姓名；
        function search(key, flag) {
            var params = {};
            if (flag == "1") {
                params = {
                    "flag": 2,
                    "phoneNo": key
                }
            } else {
                params = {
                    "flag": 2,
                    "userName": key
                }
            }
            $bb.ajax({
                url: URL + "ctrl/ptmanage/patientQueryByPhoneNum",
                data: JSON.stringify({
                    data: params
                }),
                success: function(data) {
                    if (data.success) {
                        if (data.data.rows.length > 0) {
                            $common.searchListInit({ "id": "#infoLists", "data": data.data.rows });
                        } else {
                            if (flag) {
                                $("#infoLists").html("该患者可能还没有建档");
                            }
                            if (!flag) {
                                search(key, "1");
                            }
                        }
                        $("#infoLists").addClass("show");
                        $("#infoLists").on("click", "li", function() {
                            var userId = $(this).attr("userId");
                            var obj = {
                                "babyId": userId,
                                "type": type
                            }
                            $bb.openPage("gesellDetail", obj);
                        });
                    }
                }
            });
        }
    }

    return tables;
});